package com.Akronmcp.hta.main;

import java.lang.reflect.InvocationTargetException;

import org.bukkit.entity.Player;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedChatComponent;

public class HSender {
	
	
	static
	{
		
	}
	
	public static void sendTitle(Player p,String str,String type)
	{
		sendTitle(
			    p,str,
			   type,
			    0,40,0);
	}
		
	/**发送HopTitle*/
	public static void sendTitle(
			    Player p,String str,
			   String type,
			    int fadein,int time,int fadeout)
	
	{//方法--发送数据包
		PacketContainer packet = new PacketContainer(PacketType.Play.Server.TITLE);
		//创建数据包title
		
		try
		{
		 if(p!=null)
		 {
			
			if(type.equals("TIMES"))
			{
				
				packet.getTitleActions().write(0,EnumWrappers.TitleAction.TIMES);
				packet.getIntegers().write(0,fadein);
				packet.getIntegers().write(1,time);
				packet.getIntegers().write(2,fadeout);
				//把参数写入数据包
				HM.pm.sendServerPacket(p, packet,true);
			}else if(str!=null)
			{
			   packet.getChatComponents().write(0,WrappedChatComponent.fromText(str));
			   packet.getTitleActions().write(0, EnumWrappers.TitleAction.valueOf(type));
			   HM.pm.sendServerPacket(p, packet,true); 
				
			}
			
		    
		 }else
		 {
			 if(type.equals("TIMES"))
				{
					
					packet.getTitleActions().write(0,EnumWrappers.TitleAction.TIMES);
					packet.getIntegers().write(0,fadein);
					packet.getIntegers().write(1,time);
					packet.getIntegers().write(2,fadeout);
					//把参数写入数据包
					HM.pm.broadcastServerPacket(packet);
				}else if(str!=null)
				{
					
				   packet.getChatComponents().write(0,WrappedChatComponent.fromText(str));
				   packet.getTitleActions().write(0,EnumWrappers.TitleAction.valueOf(type));

				   HM.pm.broadcastServerPacket(packet);  
				   //发送数据包给玩家,false选择不被最低级拦截	
				}
		 }
	      
		}catch(InvocationTargetException e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	/**获取tellraw包*/
	public static PacketContainer getPacket (String json)
	{
		WrappedChatComponent com = WrappedChatComponent.fromJson(json);
		PacketContainer packet = new PacketContainer(PacketType.Play.Server.CHAT);
		//创建数据包
    	packet.getChatComponents().write(0, com);
    	try{//旧版本无需
    		 packet.getChatTypes().writeDefaults();
    	     //把参数写入数据包
    	}catch (NoSuchMethodError e) {}
    	return packet;
	}
	/**发送hopchat*/
	public static void sendTellraw (Player p,PacketContainer packet)
	{//方法--发送数据包
        if(p!=null)
		 {
			try {
				HM.pm.sendServerPacket(p, packet,true);
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} 
		 }else
		 {
			HM.pm.broadcastServerPacket(packet);
		 }
    		//发送数据包给玩家,false选择不被 拦截	
	}
	

}
